using System;
using TMPro;
using Unity.Netcode;
using UnityEngine;
using UnityEngine.UI;

public class GameOverUI : MonoBehaviour
{
    [SerializeField] private TextMeshProUGUI gameOverText;
    [SerializeField] private Image bg;
    [SerializeField] private Color winColor;
    [SerializeField] private Color failColor;
    [SerializeField] private Color tieColor;
    [SerializeField] private Button restartBtn;


    private void Start()
    {
        GameManager.Instance.OnGameWin += GameManager_OnGameWin;
        GameManager.Instance.OnGameTie += GameManager_OnGameTie;
        GameManager.Instance.OnGameRematch += GameManager_OnGameRematch;

        restartBtn.onClick.AddListener(OnRestartBtnClick);

        this.gameObject.SetActive(false);
    }

    private void GameManager_OnGameTie(object sender, EventArgs e)
    {
        this.gameObject.SetActive(true);
        gameOverText.text = "GAME TIE !";
        bg.color = tieColor;
    }

    private void GameManager_OnGameRematch(object sender, EventArgs e)
    {
        this.gameObject.SetActive(false);
    }

    private void OnRestartBtnClick()
    {
        GameManager.Instance.RematchRpc();
    }

    private void GameManager_OnGameWin(object sender, GameManager.OnGameWinArgs e)
    {

        this.gameObject.SetActive(true);
        if (e.winPlayerType == GameManager.Instance.GetLocalPlayerType())
        {
            gameOverText.text = "YOU WIN !";
            bg.color = winColor;
        }
        else
        {
            gameOverText.text = "YOU FAIL !";
            bg.color = failColor;
        }
    }

}
